define(['app'],function(app){return {module: function($scope, $http, eventBusService, httpService, params) {

	//接收数据刷新
	eventBusService.subscribe('content.page.reload', function(event,data) {
		$scope.select();
	});

	httpService.post(app.urls.menu_select,{"id":params.father_id},function(data){
		if("0000" === data.code){
			$scope.menu_name = data.data[0].name;
			$scope.findButton(data.data[0].id);
		}else{
			alert(data.msg);
		}
	});

	$scope.findButton=function(menu_id){
		httpService.post(app.urls.button_select,{"menu_id":menu_id},function(data){
			if("0000" === data.code){
				$scope.buttons = data.data;
			}else{
				alert(data.msg);
			}
		});
	}

	//按钮事件
	$scope.operate = function(event) {
		$scope[event]();
	}

	//新增
	$scope.add = function (){
		let url = "modules/content/syssetting/semester/add?FatherControllerName=" + $scope.controllerName + "&fk_menuid="+params.fk_menuid+ "&father_id=" + params.father_id + "&v=" + Math.random();
		eventBusService.publish("modal.page", url);
	}
	//修改
	$scope.update = function (){
		var values = [];
		$('#' + $scope.controllerName + ' input[name="dataPk"]:checked').each(function () {
			values.push($(this).val());
		});
		if (values.length < 1) {
			eventBusService.publish('modal.alert', {"title": "操作提示", "content": "请选择修改数据。"});
		} else if (values.length > 1) {
			eventBusService.publish('modal.alert', {"title": "操作提示", "content": "您选择了多行数据，请重新选择。"});
		} else {
			let item = JSON.parse(values[0]);
			eventBusService.publish("modal.page", "modules/content/syssetting/semester/update?v=" + Math.random() + "&year_pk=" + item.year_pk +"&term_pk=" + item.term_pk + "&father_id=" + params.father_id);
		}
	}
	//删除
	$scope.delete = function (){
		var values = [];
		$('#' + $scope.controllerName + ' input[name="dataPk"]:checked').each(function () {
			values.push($(this).val());
		});
		if (values.length < 1) {
			eventBusService.publish('modal.alert', {"title": "操作提示", "content": "请选择删除的数据。"});
		} else if (values.length > 1) {
			eventBusService.publish('modal.alert', {"title": "操作提示", "content": "不能同时删除多条数据，请重新选择。"});
		} else {
			let item = JSON.parse(values[0]);
			eventBusService.publish("modal.alert",{"content":"是否删除？","isAutoClose":false,"confirm":function(){
				httpService.post(app.urls.semester_delete, item,function(data){
					if("0000" === data.code){
						$scope.find();
					}else{
						alert(data.msg);
					}
				});
			},"cancel":function(){
				//用户取消，不做操作
			}})
		}
	}

	//按钮点击事件-条件查询
	$scope.select = function(){
    	$scope.page.current = 1;
    	$scope.find();
    }
	
	//查询配置
	$scope.find = function(){
		httpService.postpage(app.urls.semester_select,$scope.form,function(data){
			if("0000" === data.code){
				$scope.rows = data.data;
			}else{
				alert(data.msg);
			}
		});
	}

	//查询学年
	$scope.getSchoolYear = function(){
		httpService.post(app.urls.getSchoolYear,{},function(data){
			if("0000" === data.code){
				$scope.schoolYearList = data.data;
			}else{
				alert(data.msg);
			}
		});
	}

	//查询学期
	$scope.getSchoolTerm = function(){
		httpService.post(app.urls.getSchoolTerm,{},function(data){
			if("0000" === data.code){
				$scope.schoolTermList = data.data;
			}else{
				alert(data.msg);
			}
			$scope.$applyAsync();
		});
	}

	let init = function (){
		$scope.find();
		$scope.getSchoolYear();
		$scope.getSchoolTerm();
	}

	init();
		//选择数据
		$scope.optData = function (element) {
			if($(element).find("input[name='dataPk']").is(":checked")){
				$(element).find("input[name='dataPk']").prop("checked",false);
				$(element).removeClass("opt_tr");
			}else{
				$(element).find("input[name='dataPk']").prop("checked",true);
				$(element).addClass("opt_tr");
			}
		}
		$scope.checkAll = function(target) {
			$("#" + $scope.controllerName + " input[name='dataPk']").each(function() {
				if ($(this).prop("checked")) {
					$(this).prop('checked', false);
					$(this).parents("tr").removeClass("opt_tr");
				} else {
					$(this).prop('checked', true);
					$(this).parents("tr").addClass("opt_tr");
				}
			})
		}
}}});